package com.xmpp.task;

import android.content.Context;
import android.os.RemoteException;
import com.iknow.IKnow;
import com.iknow.User;
import com.iknow.data.SubscribeTag;
import com.iknow.db.IKnowDataBase;
import com.iknow.http.HttpException;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.util.Loger;
import com.iknow.xmpp.service.Friend;
import com.iknow.xmpp.service.aidl.IKnowUser;
import com.iknow.xmpp.service.aidl.IXmppConnection;
import com.iknow.xmpp.service.aidl.IXmppFacade;

public class LoginAsyncTask extends GenericTask
{
  public static final int STATE_CONNECTION_RUNNING = 0;
  public static final int STATE_LOGIN_FAILED = 3;
  public static final int STATE_LOGIN_RUNNING = 1;
  public static final int STATE_LOGIN_SUCCESS = 2;
  private static final String TAG = "BeemLoginTask";
  private IXmppConnection mConnection;
  private String mErrorMessage;

  private void updateLocalUserData(String paramString1, String paramString2, String paramString3)
  {
    User localUser = new User(paramString1, paramString2, paramString3, "");
    try
    {
      String[] arrayOfString;
      int j;
      Friend localFriend = this.mConnection.getIKnowUser().getUserInfo(paramString1);
      if (localFriend != null)
      {
        IKnow.mIKnowDataBase.deleteFriend(paramString1);
        IKnow.mIKnowDataBase.addFriend(localFriend);
        localUser.setGender(localFriend.getGender());
        if ((localFriend.getImageUrl() != null) && (localFriend.getImageUrl().indexOf("loc:") == -1))
          localUser.setImgeID(localFriend.getImageUrl());
        localUser.setNick(localFriend.getName());
        localUser.setIntroduction(localFriend.getTags());
        localUser.setSignature(localFriend.getSignature());
        if (localFriend.getSubscribeTag() != null)
        {
          arrayOfString = localFriend.getSubscribeTag().split(",");
          int i = arrayOfString.length;
          j = 0;
          if (j < i)
            break label174;
        }
      }
      IKnow.mIKnowDataBase.deleteUser();
      IKnow.mIKnowDataBase.addUser(localUser);
      return;
      label174: SubscribeTag localSubscribeTag = new SubscribeTag(arrayOfString[j], "0");
      IKnow.mIKnowDataBase.addSubscribeTag(localSubscribeTag, localUser.getUID());
      ++j;
    }
    catch (RemoteException localRemoteException)
    {
      while (true)
        Loger.e("BeemLoginTask", "getUserInfo error: " + localRemoteException.getMessage());
    }
  }

  protected TaskResult _doInBackground(TaskParams[] paramArrayOfTaskParams)
  {
    TaskResult localTaskResult;
    TaskParams localTaskParams = paramArrayOfTaskParams[0];
    String str1 = null;
    String str2 = null;
    String str3 = null;
    try
    {
      if (!(IKnow.IsNetAviable()))
      {
        this.mErrorMessage = IKnow.mContext.getString(2131361878);
        Object[] arrayOfObject5 = new Object[1];
        arrayOfObject5[0] = Integer.valueOf(3);
        publishProgress(arrayOfObject5);
        localTaskResult = TaskResult.FAILED;
      }
      else
      {
        IXmppFacade localIXmppFacade = (IXmppFacade)localTaskParams.get("xmpp");
        if (localTaskParams.has("jid"))
          str1 = localTaskParams.getString("jid");
        if (localTaskParams.has("password"))
          str2 = localTaskParams.getString("password");
        if (localTaskParams.has("email"))
          str3 = localTaskParams.getString("email");
        Object[] arrayOfObject1 = new Object[1];
        arrayOfObject1[0] = Integer.valueOf(0);
        publishProgress(arrayOfObject1);
        this.mConnection = localIXmppFacade.createConnection();
        if (this.mConnection == null)
        {
          localTaskResult = TaskResult.FAILED;
        }
        else if (!(this.mConnection.connect()))
        {
          this.mErrorMessage = "网络超时";
          localTaskResult = TaskResult.FAILED;
        }
        else
        {
          Object[] arrayOfObject2 = new Object[1];
          arrayOfObject2[0] = Integer.valueOf(1);
          publishProgress(arrayOfObject2);
          if ((str1 != null) && (str2 != null));
          for (boolean bool = this.mConnection.loginWithNamePassword(str1, str2); !(bool); bool = this.mConnection.login())
          {
            this.mErrorMessage = this.mConnection.getErrorMessage();
            Object[] arrayOfObject4 = new Object[1];
            arrayOfObject4[0] = Integer.valueOf(3);
            publishProgress(arrayOfObject4);
            localTaskResult = TaskResult.FAILED;
            break label438:
          }
          this.mConnection.getIKnowUser().updateUserEmail(str3);
          if (str1 != null)
            updateLocalUserData(str1, str2, str3);
          Object[] arrayOfObject3 = new Object[1];
          arrayOfObject3[0] = Integer.valueOf(2);
          publishProgress(arrayOfObject3);
          localTaskResult = TaskResult.OK;
        }
      }
    }
    catch (RemoteException localRemoteException)
    {
      this.mErrorMessage = "Exception during connection :" + localRemoteException;
      localTaskResult = TaskResult.FAILED;
    }
    catch (IllegalStateException localIllegalStateException)
    {
      this.mErrorMessage = localIllegalStateException.getMessage();
      Loger.e("BeemLoginTask", localIllegalStateException.getMessage());
      localTaskResult = TaskResult.FAILED;
    }
    catch (HttpException localHttpException)
    {
      this.mErrorMessage = localHttpException.getMessage();
      Loger.e("BeemLoginTask", localHttpException.getMessage());
      localTaskResult = TaskResult.FAILED;
    }
    label438: return localTaskResult;
  }

  public String getErrorMessage()
  {
    return this.mErrorMessage;
  }

  protected void onCancelled()
  {
    try
    {
      if ((this.mConnection != null) && (this.mConnection.isAuthentificated()))
        this.mConnection.disconnect();
      return;
    }
    catch (RemoteException localRemoteException)
    {
      Loger.d("BeemLoginTask", "Remote exception", localRemoteException);
    }
  }
}